package com.yn.admin.controller;


import com.yn.common.model.article.ArticleContent;
import com.yn.common.model.article.ArticleTitleManage;
import com.yn.common.page.BasePage;
import com.yn.common.request.sc.*;
import com.yn.common.response.sc.BaseResult;
import com.yn.common.response.zy.DictResponse;
import com.yn.common.vo.ArticleManageVO;
import com.yn.service.service.IArticleTitleManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 栏目管理 前端控制器
 * </p>
 *
 * @author xuquanjun
 * @since 2025-06-26
 */
@RestController
@RequestMapping("api/admin/articleManage")
@Api(tags = "栏目管理")
@Slf4j
public class ArticleTitleManageAdminController {
    @Autowired
    private IArticleTitleManageService articleTitleManageService;

    /**
     *新增栏目内容
     */
    //@PreAuthorize("hasAnyAuthority('admin:articleManage:save')")
    @ApiOperation(value="新增")
    @PostMapping("/save")
    public BaseResult<String> save(@RequestBody @Validated ArticleManageRequest request){
        log.info("新增栏目内容,{}",request);
        if(articleTitleManageService.create(request)) return BaseResult.success();
        return BaseResult.failed("新增文章内容失败");
    }
    /**
     * 修改栏目内容
     */
    //@PreAuthorize("hasAuthority('admin:articleManage:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiImplicitParam(name="id", value="栏目ID")
    public BaseResult<String> update(@RequestBody @Validated ArticleManageRequest request) {
        log.info("修改栏目内容,{}", request);
        if (articleTitleManageService.updateTitleInfo(request)) {
            return BaseResult.success();
        } else {
            return BaseResult.failed();
        }
    }
    /**
     * 删除文章管理表
     * @param id Integer
     */
    //@PreAuthorize("hasAuthority('admin:articleManage:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ApiImplicitParam(name="id", value="栏目ID")
    public BaseResult<String> delete(@RequestParam(value = "id") @NotNull Long id) {
        log.info("删除文章管理表,{}", id);
        if (articleTitleManageService.deleteById(id)) {
            return BaseResult.success();
        } else {
            return BaseResult.failed();
        }
    }

    /**
     * 分页显示栏目管理表
     * @param request ArticleSearchRequest 搜索条件
     * @param pageParamRequest 分页参数
     */
    //@PreAuthorize("hasAuthority('admin:articleContent:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParam(name="keywords", value="搜索关键字")
    public BaseResult<BasePage<ArticleManageVO>> getList(ArticleManageSearchRequest request,
                                                         PageParamRequest pageParamRequest) {
        return BaseResult.success(BasePage.restPage(articleTitleManageService.getAdminList(request, pageParamRequest)));
    }

    /**
     * 根据id查询文章详情
     * */
    @ApiOperation("根据id查询栏目信息")
    @GetMapping("/info")
    public BaseResult<ArticleTitleManage> getInfo(@ApiParam("栏目id") @RequestParam @NotNull Long id) {
        return BaseResult.success(articleTitleManageService.getById(id));
    }


    /**
     * 查询字典
     * @param dictName 字典名称
     * @return 字典数据
     */
    @ApiOperation("查询字典")
    @GetMapping("/dict")
    public BaseResult<List<DictResponse>> getInfo(@ApiParam("字典类型") @RequestParam @NotNull String dictName) {
        return BaseResult.success(articleTitleManageService.showDictType(dictName));
    }


}
